<template>
	<view>
		<u-popup v-model="show" mode="center" :closeable="true" border-radius="20">
			<view class="popup">
				<view class="title">
						绑定手机号
				</view>
				<!-- <view class="button"> -->
					<button class="mobilebtn" open-type="getPhoneNumber"
						@getphonenumber="getPhoneNumber">请授权手机号</button>
				<!-- </view> -->
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getMobile,
		getOpenid,
		wxRegister
	} from "@/api/user.js"
	// import { getOpenid,wxRegister } from "@/api/user.js"
	export default {
		data() {
			return {
				show: false
			}
		},
		methods: {
			open(){
				console.log('走了吗')
				this.show = true;
			},
			close(){
				this.show = false;
			},
			//获取用户信息
			getuser (info,e) {
				
			  var _this = this;
			let data = {
				    nickName: uni.getStorageSync('nickName'),
				    avatarUrl: uni.getStorageSync('avatarUrl'),
				    openid: info.openid,
					session_key: info.session_key,
					fatherid: uni.getStorageSync("parentId") ? uni.getStorageSync("parentId") : ''
				  
				}
				console.log(data,'data')
				wxRegister(data).then(res => {
					console.log(res,'小程序登录')
					// _this.$refs.auth.close();
					uni.setStorageSync('userId',res.data.id)
					uni.setStorageSync('mobile',res.data.mobile)
					uni.setStorageSync('nickName',res.data.nickName)
					uni.setStorageSync('avatarUrl',res.data.avatarUrl)
					// uni.hideLoading();
					_this.getUserPhone(e);
					// _this.$emit('loginSuccess')
					
					
				}).catch(err => {
					
				})
			},
			getUserPhone(e){
					getMobile({
						// userId: uni.getStorageSync('userId'),
						// encryptedData: e.detail.encryptedData,
						// iv: e.detail.iv
						uid: uni.getStorageSync('userId'),
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv
					}).then(res => {
						console.log(res, '获取手机号')
						uni.hideLoading();
						if(res.errno == 0){
							uni.setStorageSync('mobile',res.data.phoneNumber);
							this.close();
							this.$emit('bindSuccess')
						}else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1000
							})
						}
						
						// this.getUserinfo();
						// uni.setStorageSync('phoneNumber', res.data.data.phoneNumber);
						
					}).catch(err => {
						uni.hideLoading();
						console.log(err, '获取手机号 失败')
						uni.showToast({
							title: res.msg,
							icon: '失败',
							duration: 1000
						})
					})
			},
			getPhoneNumber(e) {
				console.log(e.detail.errMsg) // 判断用户是否允许获取手机号
				console.log(e,'获取手机号');
				uni.showLoading({
					title: '加载中'
				})
				var that = this;
				
				uni.login({
					provider: 'weixin',
					timeout: 3000,
					success: result => {
						console.log(result,'result')
						if (result.code) {
							getOpenid({
								code: result.code
							}).then(res=>{
								console.log(res,'getopenid')
								that.getuser(res.data,e);
							})
				
						}
					},
					fail: () => {
						uni.showToast({
							title: '请求失败',
							icon: 'none'
						})
					}
				})
				
			
			
			},
		}
	}
</script>

<style>
	.popup {
		background-color: #fff;
		width: 500rpx;
		height: 300rpx;
		border-radius: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.mobilebtn {
		/* width: 260rpx; */
		height: 80rpx;
		background-color: red;
		color: #fff;
		border-radius: 16rpx;
		line-height: 80rpx;
		margin-top: 40rpx;
	}

</style>
